package proteus.servlets;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import proteus.excepcion.DAOExcepcion;
import proteus.modelo.Persona;
import proteus.negocio.PersonaNegocio;

/**
 * Servlet implementation class ValidarLoginServlet
 */
@WebServlet("/ValidarLoginServlet")
public class ValidarLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ValidarLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String Tipo="";
		String usu=request.getParameter("txtusuario");
		String pwd=request.getParameter("txtpassword");
		
		PersonaNegocio neg=new PersonaNegocio();
		
		try
		{
			Persona objPersona =neg.validar_user(usu, pwd);
			if(objPersona != null){
				//existe		
				HttpSession sesion = request.getSession();
				sesion.setAttribute("USUARIO",objPersona);
							
			}else{
				request.setAttribute("MSG_ERROR","El usuario no existe.");		
			}
			
		}
		
		catch(DAOExcepcion e){
			request.setAttribute("MSG_ERROR", e.getMessage());		
		}
		
		RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
		// Forward llama al jsp	
		rd.forward(request, response);	

	}

}
